package com.fla.millioner;

import java.io.Closeable;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.view.View;

/**
 * Contains different useful methods
 */
public class Utils {

    /**
     * Describes predicate
     */
    public interface Predicate<T> {
        boolean evaluate(T object);
    }
    
    public static void setVisibility(boolean visible, View...views) {
        for (View view : views) {
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * Filter specified list.
     * 
     * <p>
     * Creates new list and fill its with items from sourceList which are evaluated to true by specified predicate.
     * </p>
     * 
     * @param sourceList List source list.
     * @param predicate Predicate a predicate to use to evaluate list object.
     * @return List filtered list.
     */
    public static <T> List<T> filter(List<T> sourceList, Predicate<T> predicate) {
        List<T> filteredList = new LinkedList<T>();
        for (T object : sourceList) {
            if (predicate.evaluate(object)) {
                filteredList.add(object);
            }
        }
        return filteredList;
    }
    
    
    
    public static void closeQuietly(Closeable closeable) {
        try {
            if (closeable != null) {
                closeable.close();
            }
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * Shows confirmation dialog with two buttons YES and NO.
     * 
     * @param context Context to use
     * @param message CharSequence title to display
     * @param message CharSequence message to display
     * @param okClick click listener for the button Yes
     */
    public static void showOkNoDialog(Context context, CharSequence title, CharSequence message, OnClickListener okClick) {
        new AlertDialog.Builder(context)
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton("Да", okClick)
            .setNegativeButton("Нет", null)
            .show();
    }
    
    public static void showOkDialog(Context context, CharSequence title, CharSequence text, String okButtonText,
        final OnClickListener onCloseAction) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(title).setMessage(text);

        builder.setPositiveButton(okButtonText, onCloseAction);

        builder.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (onCloseAction != null) {
                    onCloseAction.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            }
        });
        builder.show();
    }
}
